<?php


namespace Module\Member\Admin\Controller;


use App\Models\Blog;
use App\Models\MemberUser;
use Illuminate\Routing\Controller;
use ModStart\Admin\Concern\HasAdminQuickCRUD;
use ModStart\Admin\Layout\AdminCRUDBuilder;
use ModStart\Core\Type\TypeUtil;
use ModStart\Field\AbstractField;
use ModStart\Grid\GridFilter;
use ModStart\Support\Concern\HasFields;
use Module\Member\Util\MemberCmsUtil;
use Module\Vendor\Type\OrderStatus;

class MemberOrderController extends Controller
{
    use HasAdminQuickCRUD;

    protected function crud(AdminCRUDBuilder $builder)
    {
        $builder
            ->init('orders')
            ->field(function ($builder) {
                /** @var HasFields $builder */
                $builder->display('id', '订单ID');
                $builder->display('sn', '订单编号');
                $builder->display('s_blog_id', '兑换账号')->hookRendering(function (AbstractField $field, $item, $index) {
                    if ($item->s_blog_id > 0) {
                        return Blog::where(['id' => $item->s_blog_id])->value("title");
                    }
                    return "无";
                });
                $builder->display('e_blog_id', '收款账号')->hookRendering(function (AbstractField $field, $item, $index) {
                    if ($item->e_blog_id > 0) {
                        return Blog::where(['id' => $item->e_blog_id])->value("title");
                    }
                    return "无";
                });
                $builder->display('user_id', '用户')->hookRendering(function (AbstractField $field, $item, $index) {
                        return MemberUser::where(['id' => $item->user_id])->value("nickname");
                });
//                $builder->display('memberUserId', '用户')->hookRendering(function (AbstractField $field, $item, $index) {
//                    return MemberCmsUtil::showFromId($item->memberUserId);
//                });
                $builder->display('money', '支付金额');
                $builder->type('add_type', '操作状态')->type(["1"=>"储蓄","2"=>"提出","3"=>"兑换"]);
                $builder->type('pay_status', '支付状态')->type(["0"=>"未支付","1"=>"已支付"]);
                $builder->type('order_status', '订单状态')->type(["0"=>"未完成","1"=>"已完成","2"=>"已取消"]);
                $builder->datetime('created_at', '创建时间');
            })
            ->gridFilter(function (GridFilter $filter) {
//                $filter->eq('id', '订单ID');
                $filter->eq('sn', '订单编号');
                $filter->eq('user_id', '用户')->selectModel('member_user', 'id', 'nickname');
//                $filter->eq('memberUserId', '用户ID');
                $filter->eq('order_status', '订单状态')->select(["0"=>"未完成","1"=>"已完成","2"=>"已取消"]);
                $filter->eq('pay_status', '支付状态')->select(["0"=>"未支付","1"=>"已支付"]);
                $filter->eq('add_type', '操作状态')->select(["1"=>"储蓄","2"=>"提出","3"=>"兑换"]);
//                $filter->eq('status', '状态')->select([
//                    OrderStatus::WAIT_PAY => TypeUtil::name(OrderStatus::class, OrderStatus::WAIT_PAY),
//                    OrderStatus::COMPLETED => TypeUtil::name(OrderStatus::class, OrderStatus::COMPLETED),
//                ]);
            })
            ->disableCUD()->canShow(false)
            ->title('用户-钱包充值订单');
    }
}
